import { Color3, Engine, SceneLoader, Vector3, Sprite, Scene, ArcRotateCamera, PointLight, StandardMaterial, Texture, ShaderMaterial, IAnimationKey, AssetsManager, MeshAssetTask, AbstractMesh, PickingInfo, Mesh } from 'babylonjs'
import 'babylonjs-loaders';


// 导入模型
export class Demo6 {
  private dom: HTMLCanvasElement
  private engine: Engine
  private scene: Scene
  private camera: ArcRotateCamera

  constructor(dom: HTMLCanvasElement) {
    this.dom = dom
    this.engine = new Engine(this.dom, true)  // 设置引擎
    this.scene = new Scene(this.engine)   // 创建场景
    this.camera = new ArcRotateCamera("Camera", 1, 0.8, 200, Vector3.Zero(), this.scene); // 创建相机
    this.camera.attachControl(this.dom, false)  // 把相机放到画布

    //光源
    const light = new PointLight("Omni", new Vector3(20, 20, 100), this.scene);
    const that = this;
    // 导入stl模型
    // SceneLoader.Append("/", "1.stl", this.scene);
    // const that = this;
    // this.scene.onPointerDown = function (evt) {
    //   const info = that.scene.pick(this.pointerX, this.pointerY)
    //   if (info?.hit) {
    //     const mesh = info.pickedMesh
    //     mesh!.outlineColor = new Color3(255,255,0)
    //   }
    // };

    let mesh;
    SceneLoader.ImportMeshAsync('m', '/', '1.stl', this.scene).then((res) => {
      const material = new StandardMaterial('new', that.scene)
      // material.diffuseColor = new Color3(255, 255, 0)
      res.meshes[0].material = material
      mesh = res.meshes[0]
    })


    window.addEventListener('resize', () => {
      this.engine.resize();
    })

    // 光源绑定到相机
    this.scene.registerBeforeRender(function () {
      light.position = that.camera.position;
    });



  }

  render() {
    // 引擎渲染场景
    this.engine.runRenderLoop(() => {
      
      this.scene.render()
    })
  }
}


